package com.wdc.queue;

public class E04Leetcode23 {
    public static void main(String[] args) {

    }


    public ListNode mergeKLists(ListNode[] lists) {
        MinHeap heap = new MinHeap(lists.length);
        //将链表的头结点加入到小顶堆
        for (ListNode h : lists) {
            if (h != null) {
                heap.offer(h);
            }
        }
        //2. 不断从堆顶移除最小元素加入新链表的尾部
        ListNode s = new ListNode(-1, null);
        ListNode t = s;
        while (!heap.isEmpty()){
            ListNode min = heap.poll();
            t.next = min;
            t = min;
            if (min.next!= null){
                heap.offer(min.next);
            }
        }

        return s.next;
    }
}
